﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Sekolah
{
    public partial class FormPSB : Form
    {
        private string _databaseConnection;
        private string _nama;
        private string _nomorFormulir;
        private string _tahunAjaran;
        public FormPSB(string nama, string nomorFormulir, string tahunAjaran)
        {
            InitializeComponent();
            _databaseConnection = _databaseConnection = AppConfig.ReadSetting(AppConfig.DATABASE_KEY);
            _nama = nama;
            _nomorFormulir = nomorFormulir;
            _tahunAjaran = tahunAjaran;
            lblNama.Text = "Nama Siswa : " + _nama;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //sp_psb_query_cicilan '061', '2016/2017'
            panel1.Controls.Clear();
            DataSet ds = new DataSet();
            using (var da = new SqlDataAdapter("sp_psb_query_cicilan '"+ _nomorFormulir+"', '"+_tahunAjaran+"'", _databaseConnection))
            {
                da.Fill(ds);
            }

            if (ds.Tables.Count == 2)
            {
                string nomorKwitansi = "Nomor Kwitansi = ";
                string total = "Total biaya PSB = ";
                string bayar = "Total pembayaran PSB = ";
                string kurang = "Kekurangan  = ";
                if (ds.Tables[1].Rows.Count > 0)
                {
                    nomorKwitansi += ds.Tables[1].Rows[0][0].ToString();
                    total += string.Format(new System.Globalization.CultureInfo("id-ID"), "Rp.{0:N0},--", int.Parse(ds.Tables[1].Rows[0][1].ToString()));
                    bayar += string.Format(new System.Globalization.CultureInfo("id-ID"), "Rp.{0:N0},--", int.Parse(ds.Tables[1].Rows[0][2].ToString()));
                    kurang += string.Format(new System.Globalization.CultureInfo("id-ID"), "Rp.{0:N0},--", int.Parse(ds.Tables[1].Rows[0][3].ToString()));
                }

                var layout = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown, BorderStyle = BorderStyle.FixedSingle };
                if (ds.Tables[0].Rows.Count > 0)
                {
                    layout.Controls.Add(new Label() { Text = nomorKwitansi, AutoSize = true });
                    layout.Controls.Add(new Label() { Text = "" });
                    layout.Controls.Add(new Label() { Text = "Daftar Transaksi Pembayaran", AutoSize = true });
                    var dg1 = new DataGridView() { AllowUserToAddRows = false, AllowUserToDeleteRows = false };
                    dg1.DataSource = ds.Tables[0];
                    layout.Controls.Add(dg1);
                    dg1.Width = panel1.Width- 10;
                    layout.Controls.Add(new Label() { Text = "" });
                    layout.Controls.Add(new Label() { Text = total, AutoSize = true });
                    layout.Controls.Add(new Label() { Text = bayar, AutoSize = true });
                    layout.Controls.Add(new Label() { Text = kurang, AutoSize = true });
                }

                panel1.Controls.Add(layout);
                layout.Dock = DockStyle.Fill;
            }
        }
    }
}
